// 引入store
import store from "../store";
import axios from "axios";
import Vue from "vue";
import router from "../router";

Vue.prototype.$http = axios;
import {
  getRole
} from "@/api/eripDataSynController.js";
import {
  updateCustomIdentify
} from "@/api/userController";
import {
  getSelect
} from "../api/workOrderController";

import btnsAuth from '@/common/btnsAuth'
window.CONFIG.btnState = btnsAuth
let btnState = window.CONFIG.btnState

const getBtnStateFun = function (userId, tenantId, btnState) {
  let params = {
    userId: userId,
    tenantId: tenantId || window.CONFIG.tenantId
  }
  getRole(params).then(res => {
    if (res.data.retCode == 200 && res.data.data) {
      let resInfo = res.data.data.resources
      // console.log(resInfo)
      // 判断当前坐席按钮权限
      for (var i = 0; i < resInfo.length; i++) {
        for (var j in btnState) {
          if (resInfo[i].code === j) {
            btnState[j] = true
          }
        }
      }
    }
  })
}
const makeForm = function (message) {
  window.eventEmitter.$emit("EventPlatform2KBP", message);
}
// 结束会话的时候清空相对应客户唯一标识
const callEnd = function (message) {
  sessionStorage.setItem("customInfo", message)
}
// 座席信息
const EventAgentInfo = function (message) {
  // 设置需要的坐席id 左侧工作台入口
  sessionStorage.setItem("userIds", message.userId);
  sessionStorage.setItem("tenantId", message.tenantId);
  // 获取坐席权限
  getBtnStateFun(message.userId, message.tenantId, btnState)
  // store.commit("userIdFun", message.userId);
  store.state.userId = message.userId;
  store.state.agentId = message.agentId;
  store.state.agentName = message.agentName;
  // 判断当前坐席
  if (
    message.cagentTypeode === "multimedia_call" ||
    message.cagentTypeode === "OmniAgent"
  ) {
    sessionStorage.setItem("multimediaCall", true); // 电话坐席
  } else if (message.agentType === "multimedia_chat") {
    sessionStorage.setItem("multimediaCall", false); // 文本坐席
  }
}
//  电话页
const EventCallRecordChange = function (message) {
  sessionStorage.setItem("customInfo", '')
  sessionStorage.setItem("workbenchInfoFun", "true");
  sessionStorage.setItem("workbenchInfos", JSON.stringify(message) || '');
  sessionStorage.setItem("workbenchInfo", JSON.stringify(message) || '');
  sessionStorage.setItem("sessionId", message.sid && message.sid !== "undefined" ? message.sid : "");
}

// 在线页签切换
const EventChatRecordChange = function (message) {
  sessionStorage.setItem("customInfo", '')
  sessionStorage.setItem("workbenchInfoFun", "true");
  sessionStorage.setItem("workbenchInfos", JSON.stringify(message) || '');
  sessionStorage.setItem("workbenchInfo", JSON.stringify(message) || '');
  sessionStorage.setItem("sessionId", message.sid && message.sid !== "undefined" ? message.sid : "");
}

// 文本结束会话
const EventChatDone = function (message) {
  sessionStorage.setItem("workbenchInfoFun", "");
  sessionStorage.setItem("workbenchInfos", "");
  sessionStorage.setItem("workbenchInfo", "");
  sessionStorage.setItem("customInfo", '')
  sessionStorage.setItem("sessionId", ""); // sessionId用来调听录音
  // sessionStorage.removeItem(message.event.sid)
}

// 电话结束会话
const EventAgentLogout = function (message) {
  sessionStorage.setItem("workbenchInfoFun", "");
  sessionStorage.setItem("workbenchInfos", "");
  sessionStorage.setItem("workbenchInfo", "");
  sessionStorage.setItem("customInfo", '')
  sessionStorage.setItem("sessionId", ""); // sessionId用来调听录音
  // sessionStorage.removeItem(message.event.sid)
}

// 业务系统客户信息、账户信息
const EventCusAndAccInfo = function (message) {
  sessionStorage.setItem("workbenchInfoFun", "true");
  sessionStorage.setItem("workbenchInfo", JSON.stringify(message));
}
// 监听工作台登出查询当前编辑中工单数量
const EventEditStatusWorkOrder = function () {
  let params = {
    classify: "",
    clientName: "",
    createTime: [],
    createUserName: "",
    currentPage: 1,
    customType: "",
    field: "",
    institution: "",
    pageSize: 10,
    priority: "",
    problemDesc: "",
    searchContent: "",
    sortTag: 1,
    source: "",
    userId: store.state.userId, // 当前用户
    workOrderFilterId: 2,
    workOrderStatusId: ""
  };
  getSelect(params).then(res => {
    let data = res.data;
    if (data.retCode === 200) {
      let lengths = data.data.list.length;
      window.parent.postMessage({
          event: "EventEditEtsNum",
          data: {
            listLength: lengths
          }
        },
        "*"
      )
    }
  })
}
const InterctClientInfo = function (message) {
  sessionStorage.setItem("customInfo", JSON.stringify(message) || '')
  let params = {
    // oldCustomIdentify: message.phone,
    hisCustomIdentify: [message.phone],
    customIdentify: message.customerId
  }
  if (message.customerId) {
    updateCustomIdentify(params).then(res => {
      if (res.data.retCode === 200) {
        console.log('更新成功')
        // 刷新交互右侧页面
        if (window.CONFIG.clearCustomerHistoryInfo) {
          store.state.typeData = 1
          router.push('/customList')
        }
      }
    })
  }
}
export {
  getBtnStateFun,
  makeForm,
  callEnd,
  EventAgentInfo,
  EventCallRecordChange,
  EventChatRecordChange,
  EventChatDone,
  EventAgentLogout,
  EventCusAndAccInfo,
  EventEditStatusWorkOrder,
  InterctClientInfo
}
